﻿using DAL;
using S7.Net;
using System;
using System.Data.SqlClient;
using System.Drawing;
using System.Net.NetworkInformation;
using System.Threading;
using System.Windows.Forms;

namespace MainOP
{
    public partial class FormDesplay : Form
    {
        public static int HH;
        public static string wellcome;
        string[] typeArray = new string[150];
        int PLC_Count = 1;
        public struct plcStructIn
        {
            public bool bool_0;
            public bool bool_1;
            public bool bool_2;
            public bool bool_3;
            public bool bool_4;
            public bool bool_5;
            public bool bool_6;
            public bool bool_7;

            public byte byte_0;
            public byte byte_1;
            public byte byte_2;//生产型号
            public byte byte_3;
            public byte byte_4;
            public byte byte_5;

            public Int16 int_0;
            public Int16 int_1;
            public Int16 int_2;
            public Int16 int_3;
            public Int16 int_4;
            public Int16 int_5;
            public Int16 int_6;
            public Int16 int_7;

            //public UInt32 Uint_0;//序列号

        }
        //----线程循环
        bool _Continue = true;
        //----PLC监控线程数组
        Thread[] PLC_Td;
        //PLC实例数组
        public SimenesPLC[] PLC_Link;
        //PLC DB结构数组
        public plcStructIn[] Struct_Test;
        public static plcStructIn _plcStruct = new plcStructIn();
        public FormDesplay()
        {
            InitializeComponent();
        }
        void dawrmap()
        {
            //创建GDI对象
            Graphics g = this.CreateGraphics();// new Graphics();
            //创建画笔对象
            Pen pen = new Pen(Brushes.Black, 3);
            //第一条横线
            Point p1 = new Point(0, 80);
            Point p2 = new Point(1920, 80);
            g.DrawLine(pen, p1, p2);
            //第二条横线
            Point p3 = new Point(0, 290);
            Point p4 = new Point(960, 290);
            g.DrawLine(pen, p3, p4);
            //第三条横线
            Point p5 = new Point(0, 500);
            Point p6 = new Point(1920, 500);
            g.DrawLine(pen, p5, p6);
            //第四条横线
            Point p7 = new Point(0, 710);
            Point p8 = new Point(960, 710);
            g.DrawLine(pen, p7, p8);
            //第五条横线
            Point p9 = new Point(0, 920);
            Point p10 = new Point(1920, 920);
            g.DrawLine(pen, p9, p10);
            //竖线
            Point p11 = new Point(960, 80);
            Point p12 = new Point(960, 920);
            g.DrawLine(pen, p11, p12);
        }

        private void FormDesplay_Load(object sender, EventArgs e)
        {
            #region 连接PLC
            PLC_Td = new Thread[PLC_Count];
            PLC_Link = new SimenesPLC[PLC_Count];
            Struct_Test = new plcStructIn[PLC_Count];
            for (int I = 0; I < PLC_Count; I++)
            {
                PLC_Td[I] = new Thread(PLC_Td_Func);
                PLC_Td[I].IsBackground = true;
                PLC_Td[I].Start(I);
            }
            #endregion
            #region 向服务器获取所有产品型号和扫码设置
            string sql = "select [typeName] from parameterSetting";
            SqlDataReader objReader = SQLHelp.GetReader(sql);
            int temp2 = 0;
            while (objReader.Read())
            {
                typeArray[temp2] = objReader["typeName"].ToString();
                temp2 += 1;
            }
            objReader.Close();
            #endregion
        }

        private void FormDesplay_Paint(object sender, PaintEventArgs e)
        {
            dawrmap();
        }
        //PLC线程

        private void PLC_Td_Func(object ob)
        {
            Ping p = new Ping();
            int num = (int)ob;
            string[] PLC_IP = new string[PLC_Count];
            PLC_IP[0] = "192.168.1.2";
            PLC_Link[num] = new SimenesPLC(CpuType.S71200, PLC_IP[num].ToString(), 0, 1);
            while (_Continue)
            {
                try
                {
                    //处理相应的PLC对象的数据处理
                    if (p.Send(PLC_IP[num], 200).Status == IPStatus.Success)//如果200ms时间间隔ping通了
                    {
                        if (PLC_Link[num].plc.IsConnected == false)//plc没有连接上
                        {
                            try
                            {
                                PLC_Link[0].plc.Open();
                                SimenesPLC.conectStatus = true;
                                
                            }
                            catch (Exception)
                            {
                                SimenesPLC.conectStatus = false;
                                
                                //throw;
                            }
                        }
                        else
                        {
                            //读取DB
                            Struct_Test[num] = (plcStructIn)PLC_Link[num].plc.ReadStruct(typeof(plcStructIn), 99, 588);
                            _plcStruct = Struct_Test[num];

                            if ((Byte)PLC_Link[num].plc.Read(DataType.DataBlock, 99, 2, VarType.Byte, 1) > 0)
                            {
                                #region 向服务器获取所有产品型号

                                string sql = "select [typeName] from parameterSetting";
                                SqlDataReader objReader = SQLHelp.GetReader(sql);
                                int temp2 = 0;
                                while (objReader.Read())
                                {

                                    typeArray[temp2] = objReader["typeName"].ToString();

                                    temp2 += 1;
                                }
                                objReader.Close();
                                #endregion
                            }
                            //欢迎
                            label32.Text = wellcome;
                            //型号
                            if (_plcStruct.byte_2 > 0)
                            {
                                label4.Text = typeArray[_plcStruct.byte_2 - 1];
                            }
                            //计划
                            label7.Text = HH + "/" + Convert.ToInt32(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 608, VarType.Int, 1));
                            //返工
                            label11.Text = (Convert.ToInt32(label20.Text) + Convert.ToInt32(label25.Text)).ToString();
                            //报废
                            label12.Text = (Convert.ToInt32(label22.Text) + Convert.ToInt32(label24.Text)).ToString();
                            //总计
                            label13.Text = (label16.Text.Split(new char[] { '/' })[0] + label26.Text.Split(new char[] { '/' })[0]) + "/" + (label16.Text.Split(new char[] { '/' })[1] + label26.Text.Split(new char[] { '/' })[1]);
                            //早晚班
                            if (Convert.ToDateTime(DateTime.Now.ToString("HH:mm")) > Convert.ToDateTime("07:59") && Convert.ToDateTime(DateTime.Now.ToString("HH:mm")) < Convert.ToDateTime("20:00"))//早班
                            {
                                //合计
                                label16.Text = _plcStruct.int_2.ToString() + "/" + _plcStruct.int_0.ToString();
                                //未完成
                                label18.Text = (_plcStruct.int_0 - _plcStruct.int_2).ToString();
                                //返工
                                label20.Text = Convert.ToInt32(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 610, VarType.Int, 1)).ToString();
                                //报废
                            }
                            else//晚班
                            {
                                //合计
                                label26.Text = _plcStruct.int_2.ToString() + "/" + _plcStruct.int_0.ToString();
                                //未完成
                                label23.Text = (_plcStruct.int_0 - _plcStruct.int_2).ToString();
                                //返工
                                label25.Text = Convert.ToInt32(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 610, VarType.Int, 1)).ToString();
                                //报废
                            }
                            //计划生产数

                            //当前生产数
                            //label11.Text = _plcStruct.int_1.ToString();
                            //合格数
                            //label13.Text = _plcStruct.int_2.ToString();
                            //不合格数
                            //label15.Text = _plcStruct.int_3.ToString();
                            ////复测数
                            //label17.Text = _plcStruct.int_4.ToString();
                            label2.Text = DateTime.Now.ToString("yyyy/MM/dd");
                            label3.Text = DateTime.Now.ToString("HH:mm:ss");
                            Thread.Sleep(100);

                            //label242.Text = "使用公司：空调国际(上海)有限公司   \t 设备公司：重庆英之拓智能设备有限公司  \t  软件版本号：V1.0";
                            //label242.Text = string.Format(label242.Text, Program.userlogin.name);
                        }

                    }
                }
                catch
                {

                }
            }
        }
    }
}
